tidy bugprone-incorrect-roundings
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sun, 20 Nov 2022 14:47:47 +0000 (07:47 -0700)
committertsteven4 <13596209+tsteven4@users.noreply.github.com>
Sun, 20 Nov 2022 14:47:47 +0000 (07:47 -0700)
manual fix, passing rounding off to Qt.  Qt may do something
smarter, or it may do just what si_round used to do.

defs.h
garmin_txt.cc
gtrnctr.cc
humminbird.cc
kml.cc
util.cc
xcsv.cc

diff --git a/defs.h b/defs.h
index 4dfd4830acfe181dbac6e565da0990d8a389746d..d59ba99bf01e6221a6e0f39705fadaefd3dcd860 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -1100,13 +1100,6 @@ gpsbabel::DateTime xml_parse_time(const QString& dateTimeString);
 
 QString rot13(const QString& s);
 
-/*
- * PalmOS records like fixed-point numbers, which should be rounded
- * to deal with possible floating-point representation errors.
- */
-
-signed int si_round(double d);
-
 /*
  * Prototypes for Endianness helpers.
  */
index e0eda354e03fe4a80352909477677002eb91af63..de62974eb205f6b16dacac4662314cf0c7567692 100644 (file)
@@ -40,7 +40,7 @@
 #include <QTextStream>             // for QTextStream
 #include <QVector>                 // for QVector
 #include <Qt>                      // for CaseInsensitive
-#include <QtGlobal>                // for qPrintable
+#include <QtGlobal>                // for qRound, qPrintable
 
 #include "csv_util.h"              // for csv_linesplit
 #include "formspec.h"              // for FormatSpecificDataList
@@ -423,7 +423,7 @@ static void
 print_course(const Waypoint* A, const Waypoint* B)             /* seems to be okay */
 {
   if ((A != nullptr) && (B != nullptr) && (A != B)) {
-    int course = si_round(waypt_course(A, B));
+    int course = qRound(waypt_course(A, B));
     *fout << QString::asprintf("%d° true", course);
   }
 }
@@ -443,7 +443,7 @@ print_distance(const double distance, const int no_scale, const int with_tab, co
       if (dist < 100.0) {
         *fout << QString::asprintf("%.1f mi", dist);
       } else {
-        *fout << QString::asprintf("%d mi", si_round(dist));
+        *fout << QString::asprintf("%d mi", qRound(dist));
       }
     }
   } else {
@@ -454,7 +454,7 @@ print_distance(const double distance, const int no_scale, const int with_tab, co
       if (dist < 100.0) {
         *fout << QString::asprintf("%.1f km", dist);
       } else {
-        *fout << QString::asprintf("%d km", si_round(dist));
+        *fout << QString::asprintf("%d km", qRound(dist));
       }
     }
   }
@@ -475,11 +475,11 @@ print_speed(const double* distance, const time_t* time)
   } else {
     unit = "kph";
   }
-  int idist = si_round(dist);
+  int idist = qRound(dist);
 
   if ((*time != 0) && (idist > 0)) {
     double speed = MPS_TO_KPH(dist / (double)*time);
-    int ispeed = si_round(speed);
+    int ispeed = qRound(speed);
 
     if (speed < 0.01) {
       *fout << QString::asprintf("0 %s", unit);
index e7c404e73ec01e2e6b4b2ba14abe9ab4238aebd5..81f1dcee673e288ab357d3841ff92f057169d887 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <QByteArray>            // for QByteArray
 #include <QDateTime>             // for QDateTime
-#include <QtGlobal>              // for qPrintable
+#include <QtGlobal>              // for qRound, qPrintable
 #include <QXmlStreamAttributes>  // for QXmlStreamAttributes
 
 #include <cstdarg>               // for va_end, va_list, va_start
@@ -242,7 +242,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata)
   }
   if (tdata.avg_hrt) {
     gtc_write_xml(1, "<AverageHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">\n");
-    gtc_write_xml(0, "<Value>%d</Value>\n", (int)(*tdata.avg_hrt + 0.5));
+    gtc_write_xml(0, "<Value>%d</Value>\n", qRound(*tdata.avg_hrt));
     gtc_write_xml(-1,"</AverageHeartRateBpm>\n");
   }
   if (tdata.max_hrt) {
@@ -252,7 +252,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata)
   }
   gtc_write_xml(0, "<Intensity>Active</Intensity>\n");
   if (tdata.avg_cad) {
-    gtc_write_xml(0, "<Cadence>%d</Cadence>\n", (int)(*tdata.avg_cad + 0.5));
+    gtc_write_xml(0, "<Cadence>%d</Cadence>\n", qRound(*tdata.avg_cad));
   }
 
   if (!gtc_course_flag) { /* activity (history) format */
index baadd27e88dfaf66b928ce4e44e190a54d70666b..3f59791055d6174ef30c275cbb04689fc1188b3b 100644 (file)
 
 #include <QMap>                 // for QMap
 #include <Qt>                   // for CaseInsensitive
+#include <QtGlobal>             // for qRound
 
 #include <cmath>                // for atan, tan, M_PI, log, sinh
 #include <cstdio>               // for snprintf, SEEK_SET
 #include <cstring>              // for strncpy, memcpy, memset
 
-#include "defs.h"               // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, si_round, xcalloc, track_add_wpt, xstrndup, mkshort, mkshort_del_handle, mkshort_new_handle, setshort_badchars, setshort_defname, setshort_length, setshort_mustuniq, setshort_...
+#include "defs.h"               // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, xcalloc, track_add_wpt, xstrndup, mkshort, mkshort_del_handle, mkshort_new_handle, setshort_badchars, setshort_defname, setshort_length, setshort_mustuniq, setshort_...
 #include "src/core/datetime.h"  // for DateTime
 
 
@@ -656,17 +657,17 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
     }
   }
 
-  hum.depth = si_round(WAYPT_GET(wpt, depth, 0)*100.0);
+  hum.depth = qRound(WAYPT_GET(wpt, depth, 0)*100.0);
   be_write16(&hum.depth, hum.depth);
 
   be_write32(&hum.time, wpt->GetCreationTime().toTime_t());
 
   double east = wpt->longitude / 180.0 * EAST_SCALE;
-  be_write32(&hum.east, si_round((east)));
+  be_write32(&hum.east, qRound((east)));
 
   double lat = geodetic_to_geocentric_hwr(wpt->latitude);
   double north = inverse_gudermannian_i1924(lat);
-  be_write32(&hum.north, si_round(north));
+  be_write32(&hum.north, qRound(north));
 
   QString name = (global_opts.synthesize_shortnames)
                    ? mkshort_from_wpt(wptname_sh, wpt)
@@ -741,9 +742,9 @@ HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt)
 
   int i = trk_head->num_points;
 
-  int32_t east = si_round(wpt->longitude / 180.0 * EAST_SCALE);
+  int32_t east = qRound(wpt->longitude / 180.0 * EAST_SCALE);
   double lat = geodetic_to_geocentric_hwr(wpt->latitude);
-  int32_t north = si_round(inverse_gudermannian_i1924(lat));
+  int32_t north = qRound(inverse_gudermannian_i1924(lat));
 
   if (wpt->creation_time.isValid()) {
     last_time = wpt->GetCreationTime().toTime_t();
@@ -768,7 +769,7 @@ HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt)
     int j = i-1;
     trk_points[j].deltaeast = east - last_east;
     trk_points[j].deltanorth = north - last_north;
-    trk_points[j].depth = si_round(WAYPT_GET(wpt, depth, 0)*100.0);
+    trk_points[j].depth = qRound(WAYPT_GET(wpt, depth, 0)*100.0);
 
     /* BE-ify */
     be_write16(&trk_points[j].deltaeast, trk_points[j].deltaeast);
diff --git a/kml.cc b/kml.cc
index 0dd315c3f5b70b0b4beac71e8ffab9f6b70e9518..548b63f8549e590d172a939177e7c6f35a860367 100644 (file)
--- a/kml.cc
+++ b/kml.cc
@@ -40,7 +40,7 @@
 #include <QVector>                      // for QVector
 #include <QXmlStreamAttributes>         // for QXmlStreamAttributes
 #include <Qt>                           // for ISODate
-#include <QtGlobal>                     // for foreach, qint64, qPrintable
+#include <QtGlobal>                     // for foreach, qint64, qRound, qPrintable
 
 #include "defs.h"
 #include "kml.h"
@@ -845,7 +845,7 @@ void KmlFormat::kml_output_point(const Waypoint* waypointp, kml_point_type pt_ty
           value = QStringLiteral("%1-none").arg(style);
         } else {
           value = QStringLiteral("%1-%2").arg(style)
-                  .arg((int)(waypointp->course / 22.5 + .5) % 16);
+                  .arg(qRound(waypointp->course / 22.5) % 16);
         }
         writer->writeTextElement(QStringLiteral("styleUrl"), value);
       } else {
diff --git a/util.cc b/util.cc
index b1337b327c9bd1f3aec83513a872cc803434f83d..b343a0dda898dbd4861c374a94a28173e354697b 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -607,16 +607,6 @@ le_write32(void* ptr, const unsigned value)
   p[3] = value >> 24;
 }
 
-signed int
-si_round(double d)
-{
-  if (d < 0) {
-    return (signed int)(d-0.5);
-  } else {
-    return (signed int)(d+0.5);
-  }
-}
-
 /*
        mkgmtime -- convert tm struct in UTC to time_t
 
diff --git a/xcsv.cc b/xcsv.cc
index 44e9cfe7846c5bd96fc53fdfb535ba533d0ea0e2..c4f21ca9492d89948ab5619306cf7b8fa995b124 100644 (file)
--- a/xcsv.cc
+++ b/xcsv.cc
@@ -44,7 +44,7 @@
 #include <QString>                    // for QString, operator+, operator==
 #include <QStringList>                // for QStringList
 #include <QTextStream>                // for QTextStream
-#include <QtGlobal>                   // for qAsConst, qPrintable
+#include <QtGlobal>                   // for qAsConst, qRound, qPrintable
 
 #include "defs.h"
 #include "csv_util.h"                 // for csv_stringtrim, dec_to_human, csv_stringclean, human_to_dec, ddmmdir_to_degrees, dec_to_intdeg, decdir_to_dec, intdeg_to_dec, csv_linesplit
@@ -1211,7 +1211,7 @@ XcsvFormat::xcsv_waypt_pr(const Waypoint* wpt)
       if (! GPS_Math_WGS84_To_UKOSMap_M(wpt->latitude, wpt->longitude, &east, &north, map))
         fatal(MYNAME ": Position (%.5f/%.5f) outside of BNG.\n",
               wpt->latitude, wpt->longitude);
-      buff = QString::asprintf(fmp.printfc.constData(), map, (int)(east + 0.5), (int)(north + 0.5));
+      buff = QString::asprintf(fmp.printfc.constData(), map, qRound(east), qRound(north));
     }
     break;
     case XcsvStyle::XT_UTM: {